#include "CServer.h"
#include "ConfigManager.h"
#include "const.h"
#include "RedisManager.h"
#include "MysqlManager.h"

//#include <mysql_driver.h>
//#include <mysql_connection.h>
//#include <cppconn/prepared_statement.h>
//#include <cppconn/resultset.h>
//#include <cppconn/statement.h>
//#include <cppconn/exception.h>

void TestRedisMgr() {
	assert(RedisManager::getInstance()->set("blogwebsite", "llfc.club"));
	std::string value = "";
	assert(RedisManager::getInstance()->get("blogwebsite", value));
	assert(RedisManager::getInstance()->get("nonekey", value) == false);
	assert(RedisManager::getInstance()->hSet("bloginfo", "blogwebsite", "llfc.club"));
	assert(RedisManager::getInstance()->hGet("bloginfo", "blogwebsite") != "");
	assert(RedisManager::getInstance()->existsKey("bloginfo"));
	assert(RedisManager::getInstance()->del("bloginfo"));
	assert(RedisManager::getInstance()->del("bloginfo"));
	assert(RedisManager::getInstance()->existsKey("bloginfo") == false);
	assert(RedisManager::getInstance()->lPush("lpushkey1", "lpushvalue1"));
	assert(RedisManager::getInstance()->lPush("lpushkey1", "lpushvalue2"));
	assert(RedisManager::getInstance()->lPush("lpushkey1", "lpushvalue3"));
	assert(RedisManager::getInstance()->rPop("lpushkey1", value));
	assert(RedisManager::getInstance()->rPop("lpushkey1", value));
	assert(RedisManager::getInstance()->lPop("lpushkey1", value));
	assert(RedisManager::getInstance()->lPop("lpushkey2", value) == false);
}


//void testMysql()
//{
//	try {
//		//初始化接口并连接
//		sql::mysql::MySQL_Driver* driver;
//		sql::Connection* con;
//		sql::Statement* stat;
//		driver = sql::mysql::get_driver_instance();
//		con = driver->connect("8.137.11.110:3308", "root", "123456");
//		con->setSchema("IMChat");//数据库名称
//		//查询
//		stat = con->createStatement();
//
//		// 
//		sql::Statement* stmt = con->createStatement();
//		//PreparedStatement* pstmt = conn->prepareStatement("SELECT * FROM temperature ORDER BY time DESC LIMIT 1");
//		sql::ResultSet* res = stmt->executeQuery("SELECT * FROM user");
//
//		// 遍历结果集，并输出查询结果
//		while (res->next()) {
//			std::string email = res->getString("email");
//			std::string name = res->getString("name");
//			std::cout << "email: " << email << ", name: " << name << std::endl;
//		}
//
//		// 释放资源
//		delete res;
//		delete stmt;
//		delete con;
//	}
//	//异常抛出
//	catch (sql::SQLException& e) {
//		std::cout << "Error: " << e.what() << std::endl;
//	}
//}


// 网关服务器
int main()
{
	// TestRedisMgr();

	// testMysql();
	std::cout << "this is GateServer......" << std::endl;

	try {
		ConfigManager& config_manager = ConfigManager::getInstance();
		std::string gate_port_str = config_manager["GateServer"]["Port"];
		std::cout << "gate_port_str = " << gate_port_str << std::endl;
		unsigned short gate_port = std::atoi(gate_port_str.c_str());

		boost::asio::io_context ioc{ 1 };
		boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
		signals.async_wait([&ioc](const boost::system::error_code& err, int signalNum) {
			if (err) {
				return;
			} 

			ioc.stop();
		});
		std::make_shared<CServer>(ioc, gate_port)->start();
		std::cout << "GateServer listen on port : " << gate_port << std::endl;
		ioc.run();
		RedisManager::getInstance()->close();
	}
	catch (std::exception& exp) {
		std::cerr << "Error:" << exp.what() << std::endl;
		RedisManager::getInstance()->close();
		return EXIT_FAILURE;
	}
}